﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Controller.Paging;
using System.IO;
using TWS.Client;
using System.Data;

public partial class admin_QuanLyDoiTac : BaseAdminPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            //tab them moi
            LoadData();
            LoadDoiTac(Convert.ToInt32(ddlLoaiBai.SelectedValue));
        }
    }

    private void LoadDoiTac(int loai)
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }

        DoiTacDataSet spDs = new DoiTacHelper().LoadDoiTac(currentPage, PagingSize, "ThuTu", "ASC", loai);

        repSanPham.DataSource = spDs.DoiTac;
        repSanPham.DataBind();

        TotalRecord = new DoiTacHelper().LoadDoiTacTheoLoai(loai).DoiTac.Rows.Count;

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }

    private void LoadData()
    {
        ddlLoaiBai.DataSource = LoaiDataHelper.LoadLoaiDoiTac(1);
        ddlLoaiBai.DataTextField = "Value";
        ddlLoaiBai.DataValueField = "Key";
        ddlLoaiBai.DataBind();

        ddlLoaiBai1.DataSource = LoaiDataHelper.LoadLoaiDoiTac(1);
        ddlLoaiBai1.DataTextField = "Value";
        ddlLoaiBai1.DataValueField = "Key";
        ddlLoaiBai1.DataBind();

        ddlLoaiBai.SelectedValue = Request.QueryString["id"];
    }

    protected void ddlLoaiBai_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int loai = Convert.ToInt32(ddlLoaiBai.SelectedValue);
        LoadDoiTac(loai);
    }

    //public string GetLoaiSanPham(object loaisanphamID)
    //{
    //    if (loaisanphamID != DBNull.Value)
    //    {
    //        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPham(Convert.ToInt32(loaisanphamID));
    //        return lspDS.LoaiSanPham[0].TenLoaiSanPham;

    //    }
    //    return "";
    //}

    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid) return;
        DoiTacHelper spHP = new DoiTacHelper();
        DoiTacDataSet spDS = new DoiTacDataSet();
        DoiTacDataSet.DoiTacRow DoiTacmoi = spDS.DoiTac.NewDoiTacRow();
        DoiTacmoi.Ten = txtTenSanPham.Text;
        if (txtLink.Text != "") DoiTacmoi.Link = txtLink.Text;
        if (txtThuTu.Text != "") DoiTacmoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        DoiTacmoi.Loai = Convert.ToInt32(ddlLoaiBai1.SelectedValue);
        UploadImage();
        if (txtHinhAnh.Text == string.Empty)
        {
            txtHinhAnh.Text = "noimage.jpg";
        }
        DoiTacmoi.HinhAnh = txtHinhAnh.Text;
        spDS.DoiTac.AddDoiTacRow(DoiTacmoi);
        spHP.SaveDoiTac(spDS);
        RedirectPage(Request.RawUrl, true);
    }

    private void UploadImage()
    {
        if (txtHinhAnh.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, txtHinhAnh.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            inputFile.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path, fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                txtHinhAnh.Text = string.Empty;
            else
            {
                txtHinhAnh.Text = fn + ".JPG";
            }
        }
    }

    protected void learnMoreButton_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(((Button)sender).CommandArgument);
        DoiTacDataSet bannerDs = new DoiTacHelper().LoadDoiTac(id);
        bannerDs.DoiTac[0].Delete();
        new DoiTacHelper().SaveDoiTac(bannerDs);
        RedirectPage(Request.RawUrl, true);
    }

    protected void repSanPham_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button repCon = ((Button)e.Item.FindControl("learnMoreButton"));
            repCon.Attributes.Add("onclick", "if (!confirm('Xóa dữ liệu?')) return;");
        }
    }

    //public string GetLoaiSanPhamName()
    //{
    //    LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
    //    LoaiSanPhamDataSet lspDs = lspHP.LoadLoaiSanPham(Convert.ToInt32(ddlCon.SelectedValue));
    //    if (lspDs.LoaiSanPham.Rows.Count == 0)
    //    {
    //        return "Tất cả";
    //    }
    //    LoaiSanPhamDataSet.LoaiSanPhamRow lsp = lspDs.LoaiSanPham[0];
    //    if (lsp.IsLoaiSanPhamChaIDNull())
    //    {
    //        return lsp.TenLoaiSanPham;
    //    }
    //    LoaiSanPhamDataSet lspDsCha = lspHP.LoadLoaiSanPham(lsp.LoaiSanPhamChaID);
    //    LoaiSanPhamDataSet.LoaiSanPhamRow lspCha = lspDsCha.LoaiSanPham[0];

    //    return lspCha.TenLoaiSanPham + "  >>  " + lsp.TenLoaiSanPham;
    //}
    public string GetTenLoai(object id)
    {
        return LoaiDataHelper.LoadLoaiDoiTac(1)[id].ToString();
    }
}